<?php
/**
 * This is Users controller file. 
 *
 * @copyright     Copyright 2008-2011, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil.controllers
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('LilAppController', 'Lil.Controller');
/**
 * UsersController class
 *
 * @uses          LilAppController
 * @package       lil
 * @subpackage    lil.controllers
 */
class UsersController extends LilAppController {
/**
 * name property
 *
 * @var string 'Users'
 * @access public
 */
	public $name = 'Users';
/**
 * uses property
 *
 * @var array
 * @access public
 */
	public $uses = array('Lil.User');
/**
 * components property
 *
 * @var array
 * @access public
 */
	//var $components = array('Cookie', 'Session', 'Auth', 'Security');
/**
 * isAuthorized method
 *
 * @return bool
 */
	public function isAuthorized() {
		if (!parent::isAuthorized()) return false;
		
		switch ($this->request->params['action']) {
			case 'admin_properties':
			case 'logout':
				return $this->currentUser->get();
				break;
			default:
				return $this->currentUser->role('admin');
		}
		
		return true;
	}
/**
 * beforeFilter method
 *
 * @return void
 */
	public function beforeFilter() {
		parent::beforeFilter();
		if (isset($this->Auth)) {
			$this->Auth->allowedActions = array('login', 'logout', 'admin_logout', 'reset', 'verify');
		}
	}
/**
 * login method
 *
 * @return void
 */
	public function login() {
		if (Configure::read('Lil.useAdminLayout')) $this->layout = 'lil_admin';
		
		if ($this->request->is('post') || env('PHP_AUTH_USER')) {
			if ($this->Auth->login()) {
				$auth_model = Configure::read('Lil.authModel');
				$auth_fields = Configure::read('Lil.authFields');
				if (!empty($this->request->data[$auth_model]['remember_me'])) {
					$f_username = $auth_fields['username'];
					$f_pass = $auth_fields['password'];
					
					$cookie = array();
					$cookie[$f_username] = $this->request->data[$auth_model][$f_username];
					$cookie[$f_pass] = $this->request->data[$auth_model][$f_pass];
					$this->Cookie->write($this->Auth->sessionKey, $cookie, true, '+2 weeks');
					unset($this->request->data[$auth_model]['remember_me']);
				}
				$this->currentArea->clear();
				$this->callPluginHandlers('after_login');
				return $this->redirect($this->Auth->redirect());
			} else {
				$this->setFlash(__d('lil', 'Username or password is incorrect'));
			}
		} else if ($cookie = $this->Cookie->read($this->Auth->sessionKey)) {
			$this->request->data = array(Configure::read('Lil.authModel') => $cookie);
			if ($this->Auth->login()) {
				// clear stale session warnings and redirect
				CakeSession::delete('Message.auth');
				$this->currentArea->clear();
				$this->callPluginHandlers('after_login');
				$this->redirect($this->Auth->redirect());
			}
		}
	}
/**
 * logout method
 *
 * @return void
 */
	public function logout() {
		if (isset($this->Auth)) {
			if ($this->Cookie->read($this->Auth->sessionKey)) {
				$this->Cookie->delete($this->Auth->sessionKey);
			}
			$this->Auth->logout();
			$this->Session->delete('Auth');
			$this->setFlash(__d('lil', 'You\'ve been logged out.'));
			$this->redirect($this->Auth->logoutRedirect);
		} else {
			$this->redirect('/');
		}
	}
/**
 * admin_logout method
 *
 * @return void
 */
	public function admin_logout() {
		$this->setAction('logout');
	}
/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->User->recursive = -1;
		$this->set('data', $this->User->find('all'));
	}
/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		$this->setAction('admin_edit');
		$this->view = 'admin_edit';
	}
/**
 * admin_edit method
 *
 * @param array $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!empty($this->request->data)) {
			if ($this->User->saveAll($this->request->data)) {
				$this->setFlash(__d('lil', 'User has been saved.'));
				return $this->doRedirect(array('action' => 'index'));
			} 
			$this->setFlash(__d('lil', 'Please verify that the information is correct.'), 'error');
		} else if (!empty($id)) {
			if ($this->request->data = $this->User->find(
				'first', 
				array(
					'conditions' => array('User.id' => $id),
					'contain'    => Configure::read('Lil.noAreas') ? null : 'Area',
					'recursive'  => 1
				)
			)) {
				unset($this->request->data['User']['passwd']);
			} else {
				return $this->error404();
			}
		}
		
		$areas = false;
		if (!Configure::read('Lil.noAreas')) {
			$Area = ClassRegistry::init('Lil.Area');
			$areas = $Area->find('list');
		}
		$this->setupRedirect();
		$this->set(compact('areas'));
	}
/**
 * admin_delete method
 *
 * @param array $id
 * @access public
 * @return void
 */
	public function admin_delete($id = null) {
		if (!empty($id) && $this->User->hasAny(array('User.id' => $id))) {
			$this->User->delete($id);
			$this->setFlash(__d('lil', 'User has been deleted.'));
			$this->redirect($this->referer());
		} else {
			$this->error404();
		}
	}
/**
 * admin_properties method
 *
 * @param array $id
 * @access public
 * @return void
 */
	public function admin_properties() {
		if (!empty($this->request->data)) {
			if ($this->User->save($this->request->data)) {
				$this->setFlash(__d('lil', 'Properties have been saved.'));
				$this->redirect(Router::url(null, true));
			} else {
				$this->setFlash(__d('lil', 'Please verify that the information is correct.'), 'error');
			}
		} else if ($this->request->data = $this->currentUser->get()) {
			unset($this->request->data['User']['passwd']);
		} else {
			$this->error404();
		}
	}
}